﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class RoleController : BaseController
    {
        BPMS_OrganizationIBLL bpms_organizationibll = new BPMS_OrganizationBLL();
        BPMS_RolesIBLL bpms_rolesibll = new BPMS_RolesBLL();
        BPMS_Roles bpms_roles = new BPMS_Roles();

        #region 角色管理
        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleIndex()
        {
            return View();
        }
        #endregion

        #region 角色所属公司
        /// <summary>
        /// 公司
        /// </summary>
        /// <returns></returns>
        public ActionResult CompanyTree()
        {
            return View();
        }
        /// <summary>
        /// 组织机构
        /// </summary>
        public ActionResult GetTree()
        {
            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_organizationibll.GetList();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            foreach (BPMS_Organization entity in itemNode)
            {
                strHtml.Append("<li>");
                strHtml.Append("<div id='" + entity.OrganizationId + "'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                //创建子节点
                strHtml.Append(GetTreeNode(list, entity.OrganizationId));
                strHtml.Append("</li>");
            }
            return Content(strHtml.ToString());
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="list">list</param>
        /// <returns></returns>
        public string GetTreeNode(IList list, string ParentId)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.Category == "公司");
            int index = 0;
            string strclass = "";
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_Organization entity in itemNode)
                {
                    if (index == 0)
                        strclass = "collapsableselected";
                    else
                        strclass = "";
                    if (entity.ParentId == ParentId)
                    {
                        sb_TreeNode.Append("<li>");
                        sb_TreeNode.Append("<div class='" + strclass + "' id='" + entity.OrganizationId + "'><img src='/Themes/images/16/chart_organisation.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                        sb_TreeNode.Append("</li>");
                    }
                    index++;
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        #endregion

        #region 角色列表
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleList()
        {
            return View();
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string orderField = Request["pqGrid_OrderField"];                           //排序字段  
            string orderType = Request["pqGrid_OrderType"];                             //排序类型
            string pqGrid_Sort = Request["pqGrid_Sort"];                                //要显示字段
            string key = Request["key"];                                                //主键
            string OrganizationId = Request["OrganizationId"];                          //公司主键
            switch (active)
            {
                case "GridBindList"://加载列表
                    Response.Write(JsonHelper.PqGridJson<BPMS_Roles>(bpms_rolesibll.GetList(OrganizationId), pqGrid_Sort));
                    Response.End();
                    break;
                case "InitRoles"://加载列表
                    Response.Write(JsonHelper.ListToJson<BPMS_Roles>(bpms_rolesibll.GetList(OrganizationId), "JSON"));
                    Response.End();
                    break;
                case "Delete":    //删除数据
                    bpms_roles = bpms_rolesibll.GetEntity(key);
                    if (bpms_roles.AllowDelete == 0)
                    {
                        Response.Write(string.Format(MessageHelper.MSG0009, bpms_roles.FullName));
                        Response.End();
                    }
                    else
                    {
                        Response.Write(bpms_rolesibll.Delete(key));
                        Response.End();
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 角色表单
        /// <summary>
        /// 角色表单
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_roles">角色信息</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_Roles bpms_roles)
        {
            string key = Request["key"];                                                //主键
            string OrganizationId = Request["OrganizationId"];                          //公司主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_roles.RoleId = key;
                bpms_roles.ModifyDate = DateTime.Now;
                bpms_roles.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_roles.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_rolesibll.Update(bpms_roles);
            }
            else
            {
                bpms_roles.OrganizationId = OrganizationId;
                bpms_roles.RoleId = CommonHelper.GetGuid;
                bpms_roles.CreateUserId = SessionHelper.GetSessionUser().UserId;
                bpms_roles.CreateUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_rolesibll.Insert(bpms_roles);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_roles = bpms_rolesibll.GetEntity(key);
                return Content(JsonHelper.ObjectToJson(bpms_roles));
            }
            return null;
        }
        /// <summary>
        /// 初始化绑定下拉框 数据字典
        /// </summary>
        public ActionResult InitBindDrop()
        {
            BPMS_ItemDetailsIBLL bpms_itemdetailsibll = new BPMS_ItemDetailsBLL();
            return Content(JsonHelper.DropToJson<BPMS_ItemDetails>(bpms_itemdetailsibll.GetList(), "JSON"));
        }
        #endregion
    }
}
